package demo;

public class Test {
    public static void main(String[] args) {
        //test1();
        //test2();
        test3();
    }
    public static void test1() {
        Animal animal = new Bird();
        animal.name = "baibai";
        animal.func();
        Bird bird = new Bird();
        Animal animal1 = bird;
        bird.name = "qinqin";
        System.out.println(animal.name);
        System.out.println(animal1.name);
        System.out.println(bird.name);
    }
    public static void test2() {
        function(new Bird());
        function(new Dog());
    }
    public static void function(Animal animal) {
        System.out.println("调用function方法");
    }
    public static Animal function2() {
        return new Bird();
    }

    public static void test3() {
        Animal animal = new Bird();
        Bird bird = (Bird)animal;
        bird.func();
        System.out.println("-------------");
        //Dog dog = (Dog)animal;//类型转换错误。
        if(animal instanceof Bird) {
            Bird bird1 = (Bird)animal;
            bird1.func();
        }else if(animal instanceof Dog) {
            Dog dog1 = (Dog)animal;
            dog1.func();
        }
    }
}
